﻿using System;
using System.IO;
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.Text;
using Microsoft.VisualStudio.TextTemplating;
using Microsoft.Win32;
using System.Reflection;

namespace MDD.PML.Utility
{
    internal class PSM2T
    {
        internal static void Transform(string upmlfile)
        {
            try
            {
                ProcessTemplate(upmlfile);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }

        private static void ProcessTemplate(string upmlFile)
        {
            string template = string.Empty;
     

            using (Stream stream = Assembly.GetExecutingAssembly()
                               .GetManifestResourceStream("Mdd.PML.Utility.umbracott.txt"))
            {
                using (StreamReader reader = new StreamReader(stream))
                {
                    template = reader.ReadToEnd();
                }
            }

            MDD.PML.Utility.CustomCmdLineHost host = new MDD.PML.Utility.CustomCmdLineHost();
            Engine engine = new Engine();
            host.TemplateFileValue = "hi";
            template = template.Replace("somefile", Path.GetFileName(upmlFile));
            string output = engine.ProcessTemplate(template, host);
            File.WriteAllText(Path.Combine(Directory.GetCurrentDirectory(), "package.xml"), output, host.FileEncoding);

            foreach (CompilerError error in host.Errors)
            {
                Console.WriteLine(error.ToString());
            }
        }
    }
}
